package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.AddressController;
import com.hmall.common.client.ItemClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.IOrderDetailService;
import com.hmall.order.service.IOrderLogisticsService;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hmall.common.utils.UserContext;


@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private IOrderDetailService detailService;

    @Autowired
    private AddressController addressController;


    @Autowired
    private IOrderLogisticsService iOrderLogisticsService;


    // 提交订单
    @Override
    public Long createOrder(RequestParams requestParams) {
        // 1）根据雪花算法生成订单id
        // Order  ok
        //    @TableId(type = IdType.ASSIGN_ID)
        //    private Long id;


        // 2）商品微服务提供 ItemClient，实现根据id查询商品的接口
        // 3）根据itemId查询商品信息
        Item item = itemClient.queryItemById(requestParams.getItemId());


        // 计算出价格 价格*商品数量
        Long price = item.getPrice() * requestParams.getNum();


        // 订单信息
        Order order = new Order();
        order.setTotalFee(price);
        order.setPaymentType(requestParams.getPaymentType());
        order.setUserId(UserContext.getUser());
        order.setStatus(1);
        // 将Order写入数据库tb_order表中
        System.out.println(order);
        save(order);

        // 7）将商品信息、orderId信息封装为OrderDetal对象，写入order_detail表
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setName(item.getName());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setNum(requestParams.getNum());
        orderDetail.setItemId(item.getId());
        orderDetail.setImage(item.getImage());
        orderDetail.setOrderId(order.getId());
        detailService.save(orderDetail);


        // 8）将user-service的根据id查询地址接口封装为FeignClient
        Long addressId = requestParams.getAddressId();
        Address address = addressController.findAddressById(addressId);
        // 9）将地址封装为OrderLogistics对象，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics();
        BeanUtils.copyProperties(address, orderLogistics);
        orderLogistics.setOrderId(order.getId());
        iOrderLogisticsService.save(orderLogistics);


        // 扣减库存接口
        try {
            itemClient.deductStock(item.getId(), Long.valueOf(requestParams.getNum()));
        } catch (Exception e) {
            throw new RuntimeException("库存不足！");
        }


        return order.getId();
    }


}
